package net.edu_soft.ec.asset.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.edu_soft.ec.asset.constant.EduConstant;
import net.edu_soft.ec.asset.mapper.FeedbackMapper;
import net.edu_soft.ec.asset.pojo.Feedback;
import net.edu_soft.ec.asset.pojo.FeedbackExample;
import net.edu_soft.ec.asset.pojo.vo.FeedbackVO;
import net.edu_soft.ec.asset.service.FeedbackService;
import net.edu_soft.ec.common.exception.OperationException;
import net.edu_soft.ec.common.exception.ParameterException;
import net.edu_soft.ec.common.util.PuingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class FeedbackServiceImpl implements FeedbackService {

    @Autowired
    private FeedbackMapper feedbackMapper;

    @Override
    public Feedback find(Integer feedBackId, Integer userInfoId) {
        return sel(feedBackId,userInfoId);
    }

    @Override
    public PageInfo<FeedbackVO> findList(Integer userInfoId, Integer pageNum, Integer pageSize) {
        PuingUtil.notNullByInt("反馈管理：分页参数不能小于等于0或者为空",pageNum,pageSize);
        PageHelper.startPage(pageNum,pageSize);
        FeedbackExample example = new FeedbackExample();
        FeedbackExample.Criteria criteria = example.createCriteria();
        //用户id不为空时则将用户id作为条件
        if (userInfoId != null && userInfoId > 0) {
            criteria.andUserInfoIdEqualTo(userInfoId);
        }
        List<FeedbackVO> feedbackVOS = new ArrayList<>();
        List<Feedback> feedbacks = feedbackMapper.selectByExample(example);
        if(feedbacks.size()>0){
            //转换
            for (Feedback feedback: feedbacks) {
                FeedbackVO feedbackVO = new FeedbackVO();
                feedbackVO.setFeedbackId(feedback.getFeedbackId());
                feedbackVO.setContent(feedback.getContent());
                feedbackVO.setUserName(feedback.getUserName());
                feedbackVO.setCreateDate(feedback.getCreateDate());
                feedbackVO.setStatus(feedback.getStatus());
                //添加
                feedbackVOS.add(feedbackVO);
            }
        }
        PageInfo<FeedbackVO> pageInfo = new PageInfo<>(feedbackVOS);
        return pageInfo;
    }

    @Override
    public int save(Feedback feedback) {
        //校验数据
        check(feedback);
        //添加日期
        feedback.setCreateDate(new Date());
        //1为未审核
        feedback.setStatus(EduConstant.STATUS_ONE);
        return feedbackMapper.insertSelective(feedback);
    }

    @Override
    public int update(Feedback feedback) {
        int i = 0;
        //校验数据
        PuingUtil.notNullByZero(feedback.getFeedbackId(),"反馈管理：反馈id不能为空或者小于等于0");
        check(feedback);
        //添加日期
        feedback.setCreateDate(new Date());
        if(feedbackMapper.selectByPrimaryKey(feedback.getFeedbackId())!=null){
            i = feedbackMapper.updateByPrimaryKeySelective(feedback);
        }else {
            throw new OperationException("反馈管理：该id："+feedback.getUserInfoId()+" 不存在反馈信息");
        }
        return i;
    }

    @Override
    public int del(String ids, Integer isDel) {
        int i = 0;
        //为0则不删除返回1
        if(isDel==0){
            return i;
        }
        Assert.hasLength(ids,"反馈管理：反馈id串不能为空");
        List<Integer> list = PuingUtil.getNumber(ids);
        if(list.size()>0){
            for (Integer feedbackId: list) {
                //查询
                Feedback feedback = feedbackMapper.selectByPrimaryKey(feedbackId);
                if(feedback==null){
                    throw new ParameterException("反馈管理：删除的id"+feedbackId+"查不到该反馈");
                }else {
                    i = feedbackMapper.deleteByPrimaryKey(feedbackId);
                }
            }
        }else {
            throw new ParameterException("反馈管理：删除的id为空");
        }

        return i;
    }

    /***
     * 查询单个反馈信息
     * @param feedBackId 反馈id
     * @param userInfoId 用户id，为空时默认为管理员账户，默认从当前账户中获取
     * @return 反馈信息
     */
    private Feedback sel(Integer feedBackId, Integer userInfoId) {
        PuingUtil.notNullByZero(feedBackId, "反馈管理：查询反馈的id不能小于等于0或者为空");
        FeedbackExample example = new FeedbackExample();
        FeedbackExample.Criteria criteria = example.createCriteria();
        //用户id不为空时则将用户id作为条件
        if (userInfoId != null && userInfoId > 0) {
            criteria.andUserInfoIdEqualTo(userInfoId);
        }
        //添加id
        criteria.andFeedbackIdEqualTo(feedBackId);
        List<Feedback> feedbacks = feedbackMapper.selectByExample(example);
        if(feedbacks.size()>0){
            return feedbacks.get(0);
        }
        return null;
    }

    /***
     * 校验反馈信息
     * @param feedback
     * @return
     */
    private void check(Feedback feedback){
        //校验用户id
        PuingUtil.notNullByZero(feedback.getUserInfoId(),"反馈管理：校验反馈者的id不能为空或者小于等于0");
        //校验反馈者账号名和信息
        Assert.hasLength(feedback.getUserName(),"反馈管理：用户姓名不能为空");
        Assert.hasLength(feedback.getContent(),"反馈管理：反馈信息不能为空");
    }
}
